
var popup={
    callback:function(id){ alert(id);
      ////  $(id).remove();
       // callBack(ret,err);
    },




    alert:function(message,Url){
        var info = '';
        if(Url=='' || Url==null || Url == undefined){
            info = '<a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary weui_btn_dialog_ok" >确定</a>';
        }else{
            info = '<a href="'+Url+'" class="weui-dialog__btn weui-dialog__btn_primary" >确定</a>';
        }

        var html = '<div class="weui_dialog_alert">' +
            '<div class="weui-mask"></div>' +
            '<div class="weui-dialog" style="top: 40%;">' +
            '<div class="weui-dialog__bd">'+message+'</div>' +
            '<div class="weui-dialog__ft">' +
            info +
            '</div>' +
            '</div>' +
            '</div>';
        $('body').append(html).on('click','.weui_btn_dialog_ok',function(){
            $('.weui_dialog_alert').remove();
        });return false;
    },
    confirm:function(message,callBack){ //alert(callBack);
        var html = '<div class="weui_dialog_confirm">' +
            '<div class="weui_mask"></div>' +
            '<div class="weui_dialog" style="top: 40%;">' +
            '<div class="weui_dialog_hd"><strong class="weui_dialog_title">温馨提示</strong></div>' +
            '<div class="weui_dialog_bd">'+message+'</div>' +
            '<div class="weui_dialog_ft">' +
            '<a href="#" id="close" class="weui_btn_dialog default" buttonIndex="1">关闭</a>' +
            '<a href="#" class="weui_btn_dialog primary" buttonIndex="2">确定</a>' +
            '</div>' +
            '</div>' +
            '</div>';
       $('body').append(html).on('click','.weui_btn_dialog',function(){
           $('.weui_dialog_confirm').remove();
           var ret =    {buttonIndex:$(this).attr('buttonIndex')};
           var err =    {'msg':'未知错误'};
           callBack(ret,err);
       });

    },

    loading:function(content){
        if(content=='' || content==null || content==undefined){
            content = '数据加载中';
        }
        var html ='<div id="loadingToast" >' +
            '<div class="weui-mask_transparent"></div>' +
                '<div class="weui-toast">' +
                    '<i class="weui-loading weui-icon_toast"></i>' +
                    '<p class="weui-toast__content">'+content+'</p>' +
            '</div>' +
        '</div>';
        $('body').append(html);
    },
    noloading:function(){
        $('#loadingToast').remove();
    }

};

var app = {
    AjaxSubmit:function(form,callBack){
        var data =  $(form).serialize();
        var Url  = $(form).attr('action');
        $.ajax({
            type:'post',
            url:Url,
            data:data,
            dataType:'json',
            beforeSend:function(XMLHttpRequest){
                popup.loading('正在提交中');
                //ShowLoading();正在提交中
            },
            success:callBack,
            complete:function(XMLHttpRequest,textStatus){
               popup.noloading();
            },
            error:function(m){
                //请求出错处理
                console.log(m);
                return false;
            }
        });
    },

    token:function(data,Url){
    $.ajax({
        type:'post',
        url:Url,
        data:data,
        dataType:'json',
        beforeSend:function(XMLHttpRequest){
           // popup.loading('正在提交中');
            //ShowLoading();正在提交中
        },
        success:function(rel){

            if(rel.status==true){ //popup.noloading();
                //window.location.href=rel.url;
                popup.alert(rel.info,rel.url);return false;
            }else{
                if(rel.afresh==true){
                    popup.loading(rel.info);
                    setTimeout(function(){app.token(data,Url)},5000);
                }else if(rel.captcha==true){
                    $('#captcha').show();$('.captcha').val('');
                    $('.token').val(rel.token);
                    popup.alert(rel.info);return false;
                }else{
                    popup.alert(rel.info);
                    return false;
                }
            }
        },
        complete:function(XMLHttpRequest,textStatus){
            popup.noloading();
        },
        error:function(){
            //请求出错处理
            popup.alert('请求出错处理');
            return false;
        }
    });
}
}
timer = 60;
function FormCountDown() {
    var get_captcha = $(".get_captcha");
    timer--;
    if (timer==-1) {
        timer = 60;
        get_captcha.removeClass('disabled').html("获取验证码");
        return;
    }
    get_captcha.addClass('disabled').html("重新获取("+timer+"s)");
    setTimeout("FormCountDown(timer)", 1000);
}
function AjaxPost(Url,serialize,callBack){
    $.ajax({
        async:false,
        type: 'POST',
        url: Url,
        data: serialize,
        cache:false,
        dataType: 'json',
        success: function(res){
            callBack(res);
        },
        complete:function(XMLHttpRequest,status){
            if(status=='timeout'){
                popup.alert('服务器繁忙，请稍后...');
            }
        }
    });
}
function AjaxGet(Url,serialize,callBack){
    $.ajax({
        async:false,
        type: 'GET',
        url: Url,
        data: serialize,
        cache:false,
        dataType: 'json',
        success: function(res){
            callBack(res);
        },
        complete:function(XMLHttpRequest,status){
            if(status=='timeout'){
                popup.alert('服务器繁忙，请稍后...');
            }
        }
    });
}
